package switchviz.graphic.signal;

import javax.swing.JPanel;

import org.jdom.Element;

import switchviz.core.signal.Signal;

public class TestSignal extends Signal implements IGaugeSignal
{
	private BasicSignalDataModel _datamodel = null;
	private BasicSignalPersistence _persistenceHandler = null;
	private TestSignalPanel _testpanel = null;
	
	private boolean _enable = true;
	
	private float _offset = 0;
	private float _coefficient = 1;
	
	private int counter = Integer.MAX_VALUE;
	private float newValue = 0.0f;
	private float oldValue = 0.0f;

	
	private static final float RATE = 120;
	
	public float[] getColor() 
	{
		return _datamodel.getColor();
	}

	public String getName() 
	{
		return _datamodel.getName();
	}

	public float getValue() 
	{
		float value = 0.0f;
	
		if (counter > RATE)
		{
			oldValue = newValue;
			newValue = ((int) (Math.random()*100))+1;
					
			counter = 0;
		}
		else
		{
			++counter;
		}
				
		value = oldValue + (counter*(newValue-oldValue)/RATE);
		
		return value*_coefficient + _offset;
	}
	
	public TestSignal(Element node)
	{
		_persistenceHandler = new BasicSignalPersistence(node);
		_datamodel = _persistenceHandler.getDataModelFromElement();
		_datamodel.setType(BasicSignalDataModel.TYPETEST);
		this.setAliasName("busyPer");
	}
	
	public TestSignal(Element node, String name)
	{
		this(node);
		_datamodel.setName(name);
		_persistenceHandler.setDataModel(_datamodel);
	}

	public Element getElement() 
	{
		return getTestPanel().getElement();
	}

	public JPanel getPanel() 
	{
		return getTestPanel();
	}

	private TestSignalPanel getTestPanel()
	{
		if(_testpanel == null)
			_testpanel = new TestSignalPanel(_persistenceHandler.getElement());
		return _testpanel;
	}

	public int getAlarmmode() 
	{
		return _datamodel.getAlarmmode();
	}

	public float getMax() 
	{
		return _datamodel.getMax();
	}

	public float getMin() 
	{
		return _datamodel.getMin();
	}
	
	public boolean isLogarithmic() 
	{
		return false;
	}

	public int getPriority() 
	{
		return _datamodel.getPriority();
	}

	public String getType() 
	{
		return _datamodel.getType();
	}

	public boolean isEnable() 
	{
		return _enable;
	}
	
	public void setOffset(float offset) 
	{
		this._offset = offset;
	}

	public void setCoefficient(float coefficient) 
	{
		this._coefficient = coefficient;
	}
}
